package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.vo.CheckGroupVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupItemService checkGroupItemService;

    @Override
    /**
     * 根据ID删除-本质上还是修改操作
     * 修改deleteFlag = 1表示数据被删除了
     * */
    public boolean deleteById(Long id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setDeleteFlag(1);
        return this.updateById(checkGroup);
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        return this.list(queryWrapper);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //pageParam 分页参数
        IPage<CheckGroup> pageParam = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);//过滤出有效数据
        if(queryPageBean.getQueryString() != null && "".equals(queryPageBean.getQueryString().trim()) == false){//判断前端页面传过来的搜索关键字是否为空
            //where delete_flag = 0
            //and (code like '' or name like '')
            queryWrapper.and(wrapper -> {
                wrapper.like("code",queryPageBean.getQueryString()).or()
                        .like("name",queryPageBean.getQueryString()).or()
                        .like("help_code",queryPageBean.getQueryString());
            });
        }
        //分页查询
        this.page(pageParam,queryWrapper);
        //分页的返回结果
        return new PageResult(pageParam.getTotal(),pageParam.getRecords());
    }

    @Override
    @Transactional
    public boolean save(CheckGroupDto checkGroupDto) {
        //1.保存检查组
        this.saveOrUpdate(checkGroupDto.getCheckGroup());
        //2.删除关系表数据
        checkGroupItemService.deleteByGroupId(checkGroupDto.getCheckGroup().getId());
        //3.维护关系数据
        List<CheckGroupItem> items = new ArrayList<>();
        for(Long itemId : checkGroupDto.getItemIds()){
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setItemId(itemId);
            checkGroupItem.setGroupId(checkGroupDto.getCheckGroup().getId());
            items.add(checkGroupItem);
        }
        checkGroupItemService.saveBatch(items);
        return true;
    }

    @Override
    public List<CheckGroupVO> findBySetmealId(Long setmealId) {

        return baseMapper.findBySetmealId(setmealId);
    }

}
